import { number } from "echarts"

export const formatTime = (
    time: string | number | Date,
    fmt: string
): string => {
    if (!time) return ''
    const date = new Date(time)
    const o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'H+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds(),
        'q+': Math.floor((date.getMonth() + 3) / 3),
        S: date.getMilliseconds(),
    }
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(
            RegExp.$1,
            (date.getFullYear() + '').substr(4 - RegExp.$1.length)
        )
    for (const k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
            fmt = fmt.replace(
                RegExp.$1,
                // @ts-ignore: Unreachable code error
                RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
            )
        }
    }
    return fmt
}

export const calcDynamicHeight = (height: (number | string)): string => {
    // * 设计稿尺寸（px）
    const baseWidth = 375;
    if (typeof(height) == 'number') {
        const calcHeight = parseFloat((window.innerWidth * height / baseWidth).toFixed(2));
        return calcHeight + 'px';
    } else {
        let newHeight = parseFloat(height.replace('px', ''));
        const calcHeight = parseFloat((window.innerWidth * newHeight / baseWidth).toFixed(2));
        return calcHeight + 'px';
    }
}

export const pxToNum = (height: string): number => {
    return parseInt(height.replace('px', ''));
}

export const calcBorderHeight = (height: (number | string)): number => {
    // * 设计稿尺寸（px）
    const baseWidth = 375;
    if (typeof(height) == 'number') {
        const calcHeight = parseFloat((window.innerWidth * height / baseWidth).toFixed(2));
        return calcHeight;
    } else {
        let newHeight = parseFloat(height.replace('px', ''));
        const calcHeight = parseFloat((window.innerWidth * newHeight / baseWidth).toFixed(2));
        return calcHeight;
    }
}